package com.example.idachuappone.index.item;

import java.util.HashMap;

import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.sina.weibo.SinaWeibo.ShareParams;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.adapter.AdapterHotItem;
import com.example.idachuappone.index.dialog.IDialog;
import com.example.idachuappone.index.entity.HotService;
import com.example.idachuappone.index.ui.MyListView;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class HotDetailActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private AdapterHotItem adapterHotItem;
	private Button btn_back;
	private Button btn_hot_phone;
	private Button btn_share;
	private BitmapUtils bu;
	private Dialog dialogLoading;
	private Dialog dialogShare;
	private Handler handler;
	private HotService hotService;
	private String id;
	private ImageView img_head;
	private LinearLayout ll_lv_last;
	private MyListView mlv_main;
	private PlatformActionListener paListener;
	private TextView tv_name;
	private TextView tv_service_flow;
	private TextView tv_slogan;

	public HotDetailActivity() {
		this.paListener = new PlatformActionListener() {
			@Override
			public void onError(Platform platform, int action, Throwable t) {
				HotDetailActivity.this.handler.sendEmptyMessage(111);
			}

			@Override
			public void onComplete(Platform arg0, int arg1, HashMap<String, Object> hashMap) {
				HotDetailActivity.this.handler.sendEmptyMessage(1111);
			}

			@Override
			public void onCancel(Platform platform, int action) {
				HotDetailActivity.this.handler.sendEmptyMessage(11111);
			}
		};
		this.handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 111 /* 111 */:
					MainToast.show(HotDetailActivity.this, "\u5206\u4eab\u5931\u8d25", 0);
				case 11111:
					MainToast.show(HotDetailActivity.this, "\u5206\u4eab\u53d6\u6d88", 0);
				default:
				}
			}
		};
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hot_detail);
		ShareSDK.initSDK(this);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.id = getIntent().getStringExtra("id");
		initView();
		initData();
	}

	private void initData() {
		this.dialogLoading.show();
		NetUtil.get(this, new StringBuilder(Constant.HOTSERVICEINFO).append(this.id).toString(), 100,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						HotDetailActivity.this.dialogLoading.dismiss();
						try {
							JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicodealipay(responseInfo.result));
							if (jsonObject.getInt("code") == 10000) {
								HotDetailActivity.this.hotService = new HotService();
								HotDetailActivity.this.hotService.parseJson(jsonObject.getJSONObject("data"));
								HotDetailActivity.this.setDataView();
								return;
							}
							MainToast.show(HotDetailActivity.this, jsonObject.getString("error"), 0);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						HotDetailActivity.this.dialogLoading.dismiss();
						MainToast.show(HotDetailActivity.this,
								HotDetailActivity.this.getResources().getString(R.string.netFail), 0);
					}
				});
	}

	private void setDataView() {
		this.tv_name.setText(this.hotService.getName());
		this.tv_slogan.setText(this.hotService.getSlogan());
		this.bu.display(this.img_head, this.hotService.getHead());
		this.ll_lv_last.setVisibility(0);
		initAdapter();
		this.tv_service_flow.setText(this.hotService.getDescription());
	}

	private void initAdapter() {
		this.adapterHotItem.setList(this.hotService.getItems());
		this.adapterHotItem.notifyDataSetChanged();
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_share = (Button) findViewById(R.id.btn_share);
		this.btn_share.setOnClickListener(this);
		this.btn_hot_phone = (Button) findViewById(R.id.btn_hot_phone);
		this.btn_hot_phone.setOnClickListener(this);
		this.mlv_main = (MyListView) findViewById(R.id.mlv_main);
		this.adapterHotItem = new AdapterHotItem(this);
		this.mlv_main.setAdapter(this.adapterHotItem);
		this.mlv_main.setOnItemClickListener(this);
		this.tv_name = (TextView) findViewById(R.id.tv_name);
		this.tv_slogan = (TextView) findViewById(R.id.tv_slogan);
		this.img_head = (ImageView) findViewById(R.id.img_head);
		this.ll_lv_last = (LinearLayout) findViewById(R.id.ll_lv_last);
		this.tv_service_flow = (TextView) findViewById(R.id.tv_service_flow);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_share /* 2131165240 */:
			this.dialogShare = createBottomShareDialog();
			this.dialogShare.show();
		case R.id.btn_hot_phone /* 2131165306 */:
			new IDialog().showPhone(this).show();
		case R.id.ll_dismiss /* 2131165441 */:
		case R.id.txt_share_sel_cancel /* 2131165630 */:
			this.dialogShare.dismiss();
		case R.id.lLayout_share_to_weixin /* 2131165622 */:
			this.dialogShare.dismiss();
			if (this.hotService != null) {
				ShareParams spweixin = new ShareParams();
				spweixin.setTitle("\u7231\u5927\u53a8" + this.hotService.getName() + "\u706b\u70ed\u9884\u8ba2\u4e2d");
				spweixin.setText("\u4e00\u573a\u79c1\u4eba\u5bb6\u5bb4\uff0c\u8ba9\u4f60\u8fdc\u79bb\u90a3\u4e9b\u7eb7\u6742\u7684\u9910\u5385\u3002\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\u3002");
				spweixin.setImageUrl(this.hotService.getItems().get(0).getFirst_img());
				spweixin.setUrl(new StringBuilder(Constant.HOT_SHARE_CONTENT).append(this.hotService.getId())
						.toString());
				spweixin.setShareType(BitmapCacheManagementTask.MESSAGE_CLEAR);
				Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
				weixin.setPlatformActionListener(this.paListener);
				weixin.share(spweixin);
			}
		case R.id.lLayout_share_to_wxpy /* 2131165624 */:
			this.dialogShare.dismiss();
			if (this.hotService != null) {
				ShareParams spweixinf = new ShareParams();
				spweixinf
						.setTitle("\u4e00\u573a\u79c1\u4eba\u5bb6\u5bb4\uff0c\u8ba9\u4f60\u8fdc\u79bb\u90a3\u4e9b\u7eb7\u6742\u7684\u9910\u5385\u3002\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\u3002");
				spweixinf.setImageUrl(this.hotService.getItems().get(0).getFirst_img());
				spweixinf.setUrl(new StringBuilder(Constant.HOT_SHARE_CONTENT).append(this.hotService.getId())
						.toString());
				spweixinf.setShareType(BitmapCacheManagementTask.MESSAGE_CLEAR);
				Platform weixinf = ShareSDK.getPlatform(WechatMoments.NAME);
				weixinf.setPlatformActionListener(this.paListener);
				weixinf.share(spweixinf);
			}
		case R.id.lLayout_share_to_qq /* 2131165626 */:
			this.dialogShare.dismiss();
			if (this.hotService != null) {
				ShareParams sp1 = new ShareParams();
				sp1.setTitle("\u7231\u5927\u53a8" + this.hotService.getName() + "\u706b\u70ed\u9884\u8ba2\u4e2d");
				sp1.setTitleUrl(new StringBuilder(Constant.HOT_SHARE_CONTENT).append(this.hotService.getId())
						.toString());
				sp1.setText("\u4e00\u573a\u79c1\u4eba\u5bb6\u5bb4\uff0c\u8ba9\u4f60\u8fdc\u79bb\u90a3\u4e9b\u7eb7\u6742\u7684\u9910\u5385\u3002\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\u3002");
				sp1.setImageUrl(this.hotService.getItems().get(0).getFirst_img());
				sp1.setSite(getString(R.string.app_name));
				sp1.setSiteUrl(new StringBuilder(Constant.HOT_SHARE_CONTENT).append(this.hotService.getId()).toString());
				Platform qzone = ShareSDK.getPlatform(QZone.NAME);
				qzone.setPlatformActionListener(this.paListener);
				qzone.share(sp1);
			}
		case R.id.lLayout_share_to_sina /* 2131165628 */:
			this.dialogShare.dismiss();
			if (this.hotService != null) {
				ShareParams sp = new ShareParams();
				sp.setText("\u4e00\u573a\u79c1\u4eba\u5bb6\u5bb4\uff0c\u8ba9\u4f60\u8fdc\u79bb\u90a3\u4e9b\u7eb7\u6742\u7684\u9910\u5385\u3002\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\u3002http://www.idachu.cn/party/info/"
						+ this.hotService.getId() + "  @\u7231\u5927\u53a8");
				sp.setImageUrl(this.hotService.getItems().get(0).getFirst_img());
				Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
				weibo.setPlatformActionListener(this.paListener);
				weibo.share(sp);
			}
		default:
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		Intent intent = new Intent(this, HotDetailGreedActivity.class);
		intent.putExtra("id", this.hotService.getItems().get(position).getId());
		intent.putExtra("hotService", this.hotService);
		startActivity(intent);
	}

	private Dialog createBottomShareDialog() {
		View view = LayoutInflater.from(this).inflate(R.layout.view_bottom_dialog_share, null);
		view.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
		view.setMinimumHeight(getWindowManager().getDefaultDisplay().getHeight());
		((LinearLayout) view.findViewById(R.id.ll_anim)).startAnimation(AnimationUtils.loadAnimation(this,
				R.anim.dialog_share_content_in));
		((LinearLayout) view.findViewById(R.id.ll_dismiss)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_qq)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_sina)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_weixin)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_wxpy)).setOnClickListener(this);
		((TextView) view.findViewById(R.id.txt_share_sel_cancel)).setOnClickListener(this);
		Dialog dialog = new Dialog(this, 2131361803);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(83);
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return dialog;
	}
}
